//  BANK acount systewm/////////////////////////////////
//Khurram Shahzad//////////////

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <string.h>
#include <cstring>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
//globaly scoped parameter//////////////
	char fname[10];		//string for first name of customer///////			
	char sname[10];		//string for secind name of customer///////			
	char nic[16];		//string for nic number of customer///////			
	int balance;		//for balance of customer///////
	int account_no;		//for account number of customer///////
	char PASSWORD[7]="NSEECS";		//password////

	int rec=0;

void create();						//creat fuction declared//////
int assign_account();				// fuction for assigning account number declared
void display(int =0);				// dislpay fuction declared
void modify();						//fuction for modification  declared
void remove();						//fuction for removing the account declared
void nsort();						//fuction for by account nuber  arranging declared//////
void sortcont();					//fuction for arrnging  declared//////
void deleteX(int);				
void sortautomatic(int);			// fuction for automatice sorting declared//////
int getlength();					//fuction for getting the length of account number declared//////
void sortcontn();					//fuction for sortiing by acc num declared//////
void sortconta();					//fuction for sorting customer's name  declared//////
void asort();					
int compare ( char *, char * );
void search();						//search fuction declared//////

///////////////////////////////////start of main function//////////////////////////////////////

void main()
{
	    char opt[2];
		char pass[100];
		int option;
		if (rec==0)
		{
			do																		
			{
				do
				{
					cout<<"Enter Admin pASSWord for access: ";
					cin>>pass;
					if (rec==3)
					{
						cout << "you have exceeded ur Password Access Limit\n SYSTEM ABORT!!! \n :-(\n";
						return;
					}
					if (strlen(pass)>7)
					{
						cout<<"ERROR: PASSWORD TOO LONG. ENTER AGAIN"<<endl;
						getch();
					}
					rec++;
				}
				while (strlen(pass)>7);
			}
			while(strcmp(pass,PASSWORD));
		}
		wyrus:
		system("cls");
		system("color f1");
		cout <<"\n\n\n\n\n";
		cout<<"\t\t\tWELCOME to the BANK ACCOUNT SYSTEM\n"<<endl				////main interface manu /////
			<<" WHAT DO U WISH US TO DO FOR U?"<<endl
			<<"\n1) Bring An Account into existence"<<endl
			<<"\n2) Display Acoutn DATA BASE"<<endl
			<<"\n3) Modify Account Balance"<<endl
			<<"\n4) Delete Account INFORMATION"<<endl
			<<"\n5) ARRNGE ACOUNTS"<<endl
			<<"\n6) SEARCH FOR ACCOUNTS"<<endl
			<<"\n7) Exit"<<endl
			<<"\n>>";

		int c=0;
		do
		{
			if (c!=0)
			cout<<"ERROR: Wrong INPUT. ENTER AGAIN"<<endl;
			cin >> opt;
			option=atoi(opt);
			c++;
		}	
		while (option==0);
		system("cls");						//clear screen function
		switch ( option )
		{
		case 1:	create(); break;
		case 2: display(); break;
		case 3: modify();break;
		case 4:	remove(); break;
		case 5: sortcont(); break;
		case 6: search();break;
		case 7: return; break;
		default: goto wyrus;
		}
}


void create()				//function for creating account//////////////
{
	char fname[10],sname[10],balanc[10],nic[16];			
	ofstream out("data.txt",ios::app);						//file handling/////
	do
	{
		cout<<"Enter First Name: ";
		cin>>fname;

		if (strlen(fname)>9)
		{
			cout<<"ERROR: STRING TOO LONG. ENTER AGAIN"<<endl;
			getch();
		}
	}
	while(strlen(fname)>9);

	do
	{
		cout<<"Enter Second Name: ";
		cin>>sname;
		if (strlen(sname)>9)
		{
			cout<<"ERROR: STRING TOO LONG. ENTER AGAIN"<<endl;
			getch();
		}
		
	}
	while(strlen(sname)>9);


	do
	{
		cout<<"Enter the National Identity Card number: ";
		cin>>nic;
		if (strlen(nic)>15)
		{
			cout<<"ERROR: STRING TOO LONG. ENTER AGAIN"<<endl;
			getch();
		}
		
	}
	while(strlen(nic)>15);





	account_no=assign_account();			//assign_account called////		
	do
	{
		do
		{

			cout << "Enter customer's starting account balance"<<endl;
			cin >> balanc;
			if (strlen(balanc)>10)
			{
				cout<<"ERROR: STRING TOO LONG. ENTER AGAIN"<<endl;
				getch();
			}
		}
		while(strlen(balanc)>10);
		balance=atoi(balanc);					//converting into integer value////
	}
	while (balance==0);

	out<<account_no<<"\t"<<fname<<"\t"<<sname<<"\t"<< balance <<"\t"<<nic<< endl;				//writing the file///

	cout << "Your account has been succesfully created :)"<<endl;
	_getch();
	main();												//return to main function////
}
/////////////////////function for assiging the account number////

int assign_account()			
{
	int account=0,no=0;
	char gar[10];
	sortautomatic(getlength());
	ifstream data1("data.txt");
	
	while (!data1.eof())
	{
		data1 >> account >>gar>>gar>>gar>>gar;					//reading the file////
		if (account-no>1)
		{
			account=no;
			break;
		}
		no=account;
	}
	account++;
	cout << "your account no is "<< account<<endl;


	return account;
}
////////////start of display function//////
void display( int f)
{
	int tbalance=0; 
	double avg;
	int	i=0;


	ifstream data("data.txt");
	cout<<setw(5)<<"Acount NO."<<setw(15)<<"First name"<<setw(15)<<"Second Name"<<setw(15)<<"Balance"<<setw(22)<<"National IDC nmbr"<<endl;
	
	while (!data.eof())
	{
		i++;
		data >> account_no >> fname >>sname >> balance>> nic;
		tbalance+=balance;
		if (!data.eof())
			cout << setw(5)<<account_no<<setw(20)<< fname<< setw(15) << sname <<setw(15)<<balance<<setw(22)<<nic<<endl;
	}
	avg=(tbalance-balance)/(i-1);
	cout<<" \n\n"<<setw(40)<<"Total Cash In Bank is::"<<(tbalance-balance)<<endl;
	cout<<" \n"<<setw(40)<<"Average Cash Per Person is::"<<avg<<endl;
	cout<<" PRESS  Any Key TO cont... "<<endl;
	getch();

	if (f==0)
		main();				//return to main function///

        	
}
////////////////start of modify function//////
void modify()
{
	display(1);					//dislpay function called////
	int no,dep,with;
	char nu[2],depo[2],wit[2];

	ifstream data("data.txt");
	ofstream writex("datax.txt");
	cout<< "Enter account no to Modify :";
	int c=0;
	do
	{
	    if (c!=0)
		cout<<"ERROR: Wrong INPUT. ENTER AGAIN"<<endl;
		cin >> nu;
		no=atoi(nu);				//converting into integer type////
		c++;
	}
	while (no==0 || no>100);
	while (!data.eof())
	{
		data >> account_no >> fname >>sname >> balance>> nic;
		if (account_no==no)
		{
			int c;
			do
		{
			cout << "Enter amount of MOney deposited :";
	
			cin >> depo;
			dep=atoi(depo);			//converting into integer type////
			cout << "Enter amount of MOney withdrawn :";
			cin >> wit;
			with=atoi(wit);			//converting into integer type////
			if (balance+dep<with)
			{
				cout << "U cannot Withdraw That Much moNEY\nTHIS IS A BANK NOT An ANATH ASHRAM \n :)\n";
				c=0;
			}
			else
				c=1;


		}
		while (c==0);
		balance=balance+dep-with;no=0;
	}
		if (!data.eof())
			writex <<account_no<<"\t"<< fname<< "\t"<< sname <<"\t"<<balance<<"\t"<<nic<<endl;
	}

	ifstream data1("datax.txt");
	ofstream writex1("data.txt");
	while (!data1.eof())
	{
	data1 >> account_no >> fname >>sname >> balance>> nic;
		if (!data1.eof())
	writex1 <<account_no<<"\t"<< fname<< "\t"<< sname <<"\t"<<balance<<"\t"<<nic<<endl;
	}
    display();						//retrun to display function/////
}

void remove()
{
	display(1);						//converting into integer type////
	int no;
	char nu[2];

	ifstream data("data.txt");
	ofstream writex("datax.txt");
	cout<< "Enter account no to Remove :";

	int c=0;
	do
	{
	    if (c!=0)
		cout<<"ERROR: Wrong INPUT. ENTER AGAIN"<<endl;
		cin >> nu;
		no=atoi(nu);				//convert into integer type//
		c++;
	}
	while (no==0 || no>100);
	while (!data.eof())
	{
		data >> account_no >> fname >>sname >> balance>> nic;
		if (account_no==no)
		{
			continue;
		}
		if (!data.eof())
			writex <<account_no<<"\t"<< fname<< "\t"<< sname <<"\t"<<balance<<"\t"<<nic<<endl;
	}

	ifstream data1("datax.txt");
	ofstream writex1("data.txt");
	while (!data1.eof())
	{
		data1 >> account_no >> fname >>sname >> balance>> nic;
		if (!data1.eof())
			writex1 <<account_no<<"\t"<< fname<< "\t"<< sname <<"\t"<<balance<<"\t"<<nic<<endl;
	}
    display();							//return to display function////
}


///////////start of sortcont function//////////////////


void sortcont()
{
	char nu[2];
	int op;
	cout<<"What do you want to do?"<<endl
		<<"1) Sort Accounts by Account Numberz"<<endl
		<<"2) Sort Accounts by  first Names"<<endl
		<<">>";
	int c=0;
	do
	{
	    if (c!=0)
		cout<<"ERROR: Wrong INPUT. ENTER AGAIN"<<endl;
		cin >> nu;
		op=atoi(nu);			
		c++;
	}
	while (op==0 || op>2);
	switch (op)
	{
		case 1:sortcontn();break;				//sortcontn function called////
		case 2:sortconta();break;				//sortconta function called////
	}
}


///////////start of sortcontn function//////////////////
void sortcontn()
{
	int no;
	no=getlength();								//getlength function called///
	
		for (int i=0;i<no;i++)					
		nsort();									//nsort function called///


	ifstream data1("datasort.dat");
	ofstream writex1("data.txt");
	while (!data1.eof())
	{
		data1 >> account_no >> fname >>sname >> balance>> nic;
		if (!data1.eof())
			writex1 <<account_no<<"\t"<< fname<< "\t"<< sname <<"\t"<<balance<<"\t"<<nic<<endl;
	}
	data1.close();									//closing the file////
	system ("del datasort.dat");					//deletig the datasort file/////	

	display();										///display function called////
}

///////////start of nsort function//////////////////


void nsort()
{
	int no=1000,bal;
	char fname1[10],sname1[10],nic1[16];

	ofstream writex("datasort.dat",ios::app);

		ifstream data("data.txt");
		while (!data.eof())
		{
			
			data >> account_no >> fname >>sname >> balance>> nic;
			if (account_no<no)
			{
				no=account_no;
				strcpy(fname1,fname);
				strcpy(sname1,sname);
				bal=balance;
				strcpy(nic1,nic);
			}
		}


		writex <<no<<"\t"<< fname1<< "\t"<< sname1 <<"\t"<<bal<<"\t"<<nic1<<endl;
		deleteX(no);							//deleteX function called///
			

}
///////////start of sortautomatic function/////////////////////////////

void sortautomatic(int no)
{

	
	for (int i=0;i<no;i++)
	nsort();								////nsort///////////
	ifstream data1("datasort.dat");
	ofstream writex1("data.txt");
	while (!data1.eof())
	{
		data1 >> account_no >> fname >>sname >> balance>> nic;
		if (!data1.eof())
			writex1 <<account_no<<"\t"<< fname<< "\t"<< sname <<"\t"<<balance<<"\t"<<nic<<endl;
	}
	data1.close();
	system ("del datasort.dat");		//////////deleting the datasort file///////////////							
}
///////////start of deleteX function/////////////////////////////


void deleteX(int no)
{

	ifstream data("data.txt");
	ofstream writex("datax.txt");
	
	while (!data.eof())
	{
	data >> account_no >> fname >>sname >> balance>> nic;
	if (account_no==no)
	{
		continue;
	}
		if (!data.eof())
	writex <<account_no<<"\t"<< fname<< "\t"<< sname <<"\t"<<balance<<"\t"<<nic<<endl;
	}

	ifstream data1("datax.txt");
	ofstream writex1("data.txt");
	while (!data1.eof())
	{
		data1 >> account_no >> fname >>sname >> balance>> nic;
		if (!data1.eof())
		writex1 <<account_no<<"\t"<< fname<< "\t"<< sname <<"\t"<<balance<<"\t"<<nic<<endl;
	}
}

///////////start of getlength function/////////////////////////////

int getlength()
{
	int no=-1,account;
	char gar[10];
	ifstream data1("data.txt");
	while (!data1.eof())
	{
	data1 >> account >>gar>>gar>>gar>>gar;
	no++;
	}

	return no;
}

///////////start sortconta function////////////////////////////////////////

void sortconta()
{
	int no;
	no=getlength();							/////getlength function called/////////////
	
		for (int i=0;i<no;i++)
		asort();							//////asort function called//////////////


	ifstream data1("datasort.dat");
	ofstream writex1("data.txt");
	while (!data1.eof())
	{
		data1 >> account_no >> fname >>sname >> balance>> nic;
		if (!data1.eof())
			writex1 <<account_no<<"\t"<< fname<< "\t"<< sname <<"\t"<<balance<<"\t"<<nic<<endl;
	}
	data1.close();
	system ("del datasort.dat");
	display();						////////dispaly function called/////////////
}

///////////start asort function////////////////////////////////////////

void asort()
{
	int no,bal,a;
	char fname1[10]="zzzzzzzzz",sname1[10],nic1[16];

	ofstream writex("datasort.dat",ios::app);

	ifstream data("data.txt");
	
	while (!data.eof())
	{
			
		data >> account_no >> fname >>sname >> balance>> nic;
		a=strcmp(fname,fname1);					////compare fname1 and fname/////
		if (a!=-1)
				a=0;
		if (a)
		{
			no=account_no;
			strcpy(fname1,fname);
			strcpy(sname1,sname);
			bal=balance;
			strcpy(nic1,nic);
		}
	}
	writex <<no<<"\t"<< fname1<< "\t"<< sname1 <<"\t"<<bal<<"\t"<<nic1<<endl;
	deleteX(no);												///////deleteX funtion called/////////
	
}
///////////start search function////////////////////////////////////////

void search()
{
	char sername[15];
	cout<<"Enter Name To Search: ";
	cin>>sername;
	ifstream data("data.txt");
	int i=0;
	while (!data.eof())
	{
		data>>account_no>>fname>>sname>>balance>>nic;
		if( compare(fname,sername)|| compare(sname,sername))
		{
			if (!data.eof())
				cout << setw(5)<<account_no<<setw(20)<< fname<< setw(15) << sname <<setw(15)<<balance<<setw(20)<<nic<<endl;
		i++;
		}
	}
	if ( i==0 )
		cout<<setw(12)<<"NONE FOUND"<<endl;
	getch();
	main();
}
///////////start of COMPARE function////////////////////////////////////////
int compare ( char *name, char *sname )
{
	int j=0;
	for ( int i=0; i<3; i++ )
	{
		if ( *name++==*sname++ )
			j++;
	}
	if ( j==3 )
		return 1;
	else
		return 0;
}

////////////////////////////////////////////////////////////////////END////////////////////////////////////////////